/*
	MainWindow.cpp

	contains the code pertaining to the main window of the toolkit application

	Author: Aidan Goddard 23/7/14
*/

#include<Window.h>

///////////////////////////////////////////////////////////////////////////////////////
// internal values
static Snowstorm::Window *main_wnd;

// STATE_NO_WORLD buttons
#define IDM_FILE_NEW_STATE_NO_WORLD 01
#define IDM_FILE_OPEN_STATE_NO_WORLD 02
#define IDM_FILE_EXIT_STATE_NO_WORLD 03

#define IDM_HELP_CONTENTS_STATE_NO_WORLD 04
#define IDM_HELP_ABOUT_STATE_NO_WORLD 05

// STATE_NO_WORLD menu items
static HMENU menu_no_world;
static HMENU menu_no_world_file;
static HMENU menu_no_world_help;

///////////////////////////////////////////////////////////////////////////////////////
// button callback
static void button_callback(Snowstorm::Window *wnd, WPARAM wparam, LPARAM lparam)
{
	// check which button was clicked
	switch(LOWORD(wparam))
	{
		///////////////////////////////
		// STATE_NO_WORLD
	case IDM_FILE_NEW_STATE_NO_WORLD:
		break;
	case IDM_FILE_OPEN_STATE_NO_WORLD:
		break;
	case IDM_FILE_EXIT_STATE_NO_WORLD:
		break;
	case IDM_HELP_CONTENTS_STATE_NO_WORLD:
		break;
	case IDM_HELP_ABOUT_STATE_NO_WORLD:
		break;

		///////////////////////////////
		// STATE_WORLD


	}

	// finished
	return;
}

///////////////////////////////////////////////////////////////////////////////////////
// quit callback
//void(*handler)(Window *wnd, WPARAM, LPARAM)
static void quit_callback(Snowstorm::Window *wnd, WPARAM wparam, LPARAM lparam)
{
	// check save system
	if(true)
	{
		if(MessageBox(wnd->GetHWND(), L"Close the world without saving changes?", L"Unsaved changes!", MB_ICONWARNING | MB_YESNO | MB_DEFBUTTON2) == IDNO)
			return;
	}

	// quit
	PostQuitMessage(0);
}

///////////////////////////////////////////////////////////////////////////////////////
// the create function
void create_main_window()
{
	// create the main window
	Snowstorm::WindowAttributes attr_winmain;
	attr_winmain.x = 100;
	attr_winmain.y = 100;
	attr_winmain.width = 1024;
	attr_winmain.height = 768;
	attr_winmain.title = L"Toolkit";
	attr_winmain.parent_hwnd = NULL;
	attr_winmain.catch_input = true;
	attr_winmain.window_type = Snowstorm::TYPE_POPUP;
	main_wnd = Snowstorm::Window::Create(&attr_winmain);
	main_wnd->Show();

	// register the callback functions
	main_wnd->RegisterHandler(WM_CLOSE, quit_callback);
	main_wnd->RegisterHandler(WM_COMMAND, button_callback);

	// create STATE_NO_WORLD menus
	menu_no_world = CreateMenu();
	menu_no_world_file = CreateMenu();
	menu_no_world_help = CreateMenu();

	// file
	AppendMenuW(menu_no_world_file, MF_STRING, IDM_FILE_NEW_STATE_NO_WORLD, L"New");
	AppendMenuW(menu_no_world_file, MF_STRING, IDM_FILE_OPEN_STATE_NO_WORLD, L"Open");
	AppendMenuW(menu_no_world_file, MF_SEPARATOR, 0, NULL);
	AppendMenuW(menu_no_world_file, MF_STRING, IDM_FILE_EXIT_STATE_NO_WORLD, L"Exit");
	AppendMenuW(menu_no_world, MF_POPUP, (UINT_PTR)menu_no_world_file, L"File");

	// help
	AppendMenuW(menu_no_world_help, MF_STRING, IDM_HELP_CONTENTS_STATE_NO_WORLD, L"Contents");
	AppendMenuW(menu_no_world_help, MF_STRING, IDM_HELP_ABOUT_STATE_NO_WORLD, L"About");
	AppendMenuW(menu_no_world, MF_POPUP, (UINT_PTR)menu_no_world_help, L"Help");

	// set the menu
	SetMenu(main_wnd->GetHWND(), menu_no_world);

	// finished
	return;
}